import { defineStore } from "pinia";
import { ref } from "vue";

/**
 * 全局数据同步store
 * 用于管理跨页面的数据更新通知
 */
export const useDataSyncStore = defineStore("dataSync", () => {
  // 数据更新时间戳，用于触发响应式更新
  const zoneDataTimestamp = ref(Date.now());
  const userDataTimestamp = ref(Date.now());
  const channelDataTimestamp = ref(Date.now());
  const deviceDataTimestamp = ref(Date.now());
  const categorizeDataTimestamp = ref(Date.now());

  /**
   * 通知分区数据已更新
   */
  function notifyZoneDataUpdated() {
    zoneDataTimestamp.value = Date.now();
    console.log(
      "[DataSync] Zone data updated at:",
      new Date().toLocaleTimeString()
    );
  }

  /**
   * 通知用户数据已更新
   */
  function notifyUserDataUpdated() {
    userDataTimestamp.value = Date.now();
    console.log(
      "[DataSync] User data updated at:",
      new Date().toLocaleTimeString()
    );
  }

  /**
   * 通知频道数据已更新
   */
  function notifyChannelDataUpdated() {
    channelDataTimestamp.value = Date.now();
    console.log(
      "[DataSync] Channel data updated at:",
      new Date().toLocaleTimeString()
    );
  }

  /**
   * 通知设备数据已更新
   */
  function notifyDeviceDataUpdated() {
    deviceDataTimestamp.value = Date.now();
    console.log(
      "[DataSync] Device data updated at:",
      new Date().toLocaleTimeString()
    );
  }

  /**
   * 通知分类数据已更新
   */
  function notifyCategorizeDataUpdated() {
    categorizeDataTimestamp.value = Date.now();
    console.log(
      "[DataSync] Categorize data updated at:",
      new Date().toLocaleTimeString()
    );
  }

  return {
    // 时间戳
    zoneDataTimestamp,
    userDataTimestamp,
    channelDataTimestamp,
    deviceDataTimestamp,
    categorizeDataTimestamp,
    // 通知方法
    notifyZoneDataUpdated,
    notifyUserDataUpdated,
    notifyChannelDataUpdated,
    notifyDeviceDataUpdated,
    notifyCategorizeDataUpdated
  };
});
